﻿using FarmSmartSleep.Common.Domain;
using FarmSmartSleep.Common.Dtos;
using FarmSmartSleep.Common.Infrastructure;
using MediatR;

namespace FarmSmartSleep.Organ.API.Application.Commands.CottagesCommands
{
    public class FieldCreateHander : IRequestHandler<FieldCreateCommand, Result>
    {

        private readonly IBaseRepository<Fields> fieldsRepository;
        private readonly ILogger<FieldCreateHander> logger;

        public FieldCreateHander(IBaseRepository<Fields> fieldsRepository, ILogger<FieldCreateHander> logger)
        {
            this.fieldsRepository = fieldsRepository;
            this.logger = logger;
        }

        /// <summary>
        /// 添加栏位信息
        /// </summary>
        /// <param name="request">添加栏位请求对象</param>
        /// <param name="cancellationToken"></param>
        /// <returns>添加栏位响应对象</returns>
        public async Task<Result> Handle(FieldCreateCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var result = new Result();
                //判断单挑还是批量添加
                if (!string.IsNullOrEmpty(request.FieIdName))
                {
                    var info = fieldsRepository.GetAll().FirstOrDefault(c => c.FieIdName == request.FieIdName&&c.CottageId==request.CottageId);
                    if (info != null)
                    {
                        result.Code = -1;
                        result.Messege = "该栋舍下已经存在该名称的栏位";
                        return result;
                    }
                    else
                    {
                        Fields fields = new Fields();
                        fields.FieIdName = request.FieIdName;
                        fields.CottageId = request.CottageId;
                        result.Code = fieldsRepository.Add(fields);
                        result.Messege = "栏位添加成功";
                    }
                }
                else
                {
                    int n = request.GetNumber;
                    for (int i = 1; i <= n; i++)
                    {
                        Fields fields = new Fields();
                        fields.FieIdName = request.Prefix + request.FieldNumber.ToString();
                        var info = fieldsRepository.GetAll().FirstOrDefault(c => c.FieIdName == fields.FieIdName && c.CottageId == request.CottageId);
                        if (info != null)
                        {
                            result.Code = -1;
                            result.Messege = "该栋舍里名称为"+ fields.FieIdName +"的栏位已经存在，已生成"+(i-1)+"条数据，往后数据停止生成";
                            return result;
                        }
                        fields.CottageId = request.CottageId;
                        result.Code += fieldsRepository.Add(fields);
                        request.FieldNumber++;
                    }
                    result.Messege = n + "个栏位添加成功";
                }
                logger.LogInformation("添加栏位信息方法调用成功");
                return result;
            }
            catch (Exception ex)
            {
                logger.LogError("添加栏位信息方法调用出错" + ex);
                throw;
            }
        }
    }
}
